#include "game.h"
#include "physic.h"
#include <SDL/SDL_mixer.h>

s_player    *g_player_1;
char        **g_skin_list;
s_weapon    **g_weapons;
s_wlist     *g_wlist;

int keymap(int play,
           SDL_Event event,
           SDL_Surface  **screen,
           int           ev,
           int          *i)
{
  switch (event.type)
    {
    case SDL_QUIT:
      play = 0;

      break;
    case SDL_KEYDOWN:
      switch(event.key.keysym.sym)
      {
      case SDLK_UP:
	g_player_1->coord.y -= 10;
	break;
      case SDLK_DOWN:
	g_player_1->coord.y += 10;
	break;
      case SDLK_RIGHT:
	g_player_1->coord.x += 10;
	break;
      case SDLK_LEFT:
	g_player_1->coord.x -= 10;
	break;
      case SDLK_ESCAPE:
	play = 0;
	break;
      case SDLK_SPACE:
        if (ev == 1)
            shoot(g_weapons[(*i) % 4], screen);
        break;
      case SDLK_RCTRL:
        (*i)++;
        break;
      default:
	break;
      }
      break;
    }
  return (play);
}

int game(SDL_Surface *screen)
{
  int play = 1;
  SDL_Event event;

    int ev = 1;
    int i = 0;

  while (play)
  {
    ev = SDL_PollEvent(&event);
    background_update(screen);
    g_skin_list = init_skin_list();
    if (g_weapons == NULL)
      g_weapons = init();
    play = player_update(screen);
    play = keymap(play, event, &screen, ev, &i);
    put_monster(screen);
    update_wlist(g_wlist, &screen);
    player_col(NULL);
    monster_col(g_wlist);
    put_monster(screen);
    SDL_Flip(screen);
  }
  return(0);
}

int sdl_init()
{
  int           widht = 800;
  int           height = 800;
  int           nb_color = 0;
  SDL_Surface   *screen = NULL;


  SDL_Init(SDL_INIT_EVERYTHING);
  screen = SDL_SetVideoMode(widht, height, nb_color, SDL_HWSURFACE
			    | SDL_RESIZABLE | SDL_DOUBLEBUF);
  background_init(screen->w, screen->h);
  g_skin_list = init_skin_list();
  g_weapons = init();
  player_init(800, 800);
  make_monster();
  game(screen);
  SDL_Quit();
  return (0);
}

int main(int argc, char **argv)
{
  argc = argc;
  argv = argv;
  sdl_init();
  return (0);
}
